package com.touchportfolio.model
{
	
	import com.touchportfolio.VO.MediaVo;
	import com.touchportfolio.VO.NodeVO;
	import com.touchportfolio.events.PayloadEvent;
	import com.touchportfolio.loadQueue.loaders.AMFCaller;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	[Event(name="active_row_change", type="ApplicationModel")]
	[Event(name="grid_data_loaded", type="ApplicationModel")]
	[Event(name="active_row_open", type="ApplicationModel")]
	[Event(name="active_row_close", type="ApplicationModel")]
	[Event(name="drag_attract_on", type="ApplicationModel")]
	[Event(name="drag_attract_off", type="ApplicationModel")]
	[Event(name="next_image", type="ApplicationModel")]
	[Event(name="previous_image", type="ApplicationModel")]
	[Event(name="play_video", type="ApplicationModel")]
	[Event(name="stop_video", type="ApplicationModel")]
	[Event(name="setting_change", type="ApplicationModel")]
	[Event(name="sound_change", type="ApplicationModel")]
	[Event(name="fullscreen_change", type="ApplicationModel")]
	[Event(name="start_slideshow_drag", type="ApplicationModel")]
	[Event(name="stop_slideshow_drag", type="ApplicationModel")]
	[Event(name="color_change", type="ApplicationModel")]
	
	public class ApplicationModel extends EventDispatcher
	{
		public var _portfolioData:Array;
		
		public static const ACTIVE_ROW_CHANGE:String = "active_row_change";
		public static const GRID_DATA_LOADED:String = "grid_data_loaded";
		public static const ACTIVE_ROW_OPEN:String = "active_row_open";
		public static const ACTIVE_ROW_CLOSE:String = "active_row_close";
		public static const DRAG_ATTRACT_ON:String = "drag_attract_on";
		public static const DRAG_ATTRACT_OFF:String = "drag_attract_off";
		public static const NEXT_IMAGE:String = "next_image";
		public static const PREVIOUS_IMAGE:String = "previous_image";
		public static const PLAY_VIDEO:String = "play_video";
		public static const STOP_VIDEO:String = "stop_video";
		public static const FULLSCREEN_CHANGE:String = "fullscreen_change";
		public static const SOUND_CHANGE:String = "sound_change";
		public static const SETTING_CHANGE:String = "setting_change";
		public static const START_SLIDESHOW_DRAG:String = "start_slideshow_drag";
		public static const STOP_SLIDESHOW_DRAG:String = "stop_slideshow_drag";
		public static const COLOR_CHANGE:String = "color_change";

		

		/** GETTERS - SETTERS **/
		
		private var _activeRow:int;
		public function get activeRow():int
		{
			return _activeRow;
		}

		public function set activeRow(value:int):void
		{
			if(activeRowOpen) activeRowOpen = false;
			
			_activeRow = value;
			
			if(_activeRow >= _totalRows) _activeRow=totalRows-1;
			else if(_activeRow<0) _activeRow = 0;
			
			dispatchEvent(new Event(ACTIVE_ROW_CHANGE));
		}
		
		private var _activeRowOpen:Boolean = false;
		public function get activeRowOpen():Boolean
		{
			return _activeRowOpen;
		}

		public function set activeRowOpen(value:Boolean):void
		{
			if(value && !_activeRowOpen)
			{
				_activeRowOpen = value;
				dispatchEvent(new Event(ACTIVE_ROW_OPEN));	
			}
			else if(!value && _activeRowOpen)
			{
				_activeRowOpen = value;
				dispatchEvent(new Event(ACTIVE_ROW_CLOSE));	
			}
		}
		
		private var _dragAttract:Boolean;
		public function get dragAttract():Boolean
		{
			return _dragAttract;
		}

		public function set dragAttract(value:Boolean):void
		{
			_dragAttract = value;
			if(_dragAttract) dispatchEvent(new Event(DRAG_ATTRACT_ON));
			else dispatchEvent(new Event(DRAG_ATTRACT_OFF));
		}
		
		private var _slideshowDrag:Boolean = false;
		public function get slideshowDrag():Boolean
		{
			return _slideshowDrag;
		}

		public function set slideshowDrag(value:Boolean):void
		{
			if(value != _slideshowDrag)
			{
				if(value)
				{
					dispatchEvent(new Event(START_SLIDESHOW_DRAG));
				}
				else
				{
					dispatchEvent(new Event(STOP_SLIDESHOW_DRAG));
				}
			}
			_slideshowDrag = value;
		}

		
		private var _totalImages:Number = 0;
		public function get totalImages():Number
		{
			return _totalImages;
		}

		public function set totalImages(value:Number):void
		{
			_totalImages = value;
		}
		
		private var _color:Number = 0x518564;
		public function get color():Number
		{
			return _color;
		}
		
		public function set color(value:Number):void
		{
			_color = value;
			dispatchEvent(new Event(COLOR_CHANGE));
		}


		private var _activeImage:int = 0;
		public function get activeImage():int
		{
			return _activeImage;
		}

		public function set activeImage(value:int):void
		{
			if(value > _activeImage)
			{
				if(value < _totalImages)
				{
					_activeImage = value;
					dispatchEvent(new Event(NEXT_IMAGE));
				}
			}
			else if(value < _activeImage)
			{
				if(value >= 0)
				{
					_activeImage = value;
					dispatchEvent(new Event(PREVIOUS_IMAGE));
				}
			}
			
			dispatchEvent(new Event(NEXT_IMAGE));
		}
		
		
		private var _slideshowReady:Boolean;
		public function get slideshowReady():Boolean
		{
			return _slideshowReady;
		}

		public function set slideshowReady(value:Boolean):void
		{
			_slideshowReady = value;
		}

		
		private var _totalRows:int;
		public function get totalRows():int
		{
			return _totalRows;
		}

		public function set totalRows(value:int):void
		{
			_totalRows = value;
		}
		
		private var _nodes:Array = new Array();
		public function get nodes():Array
		{
			return _nodes;
		}

		public function set nodes(value:Array):void
		{
			_nodes = value;
		}

		
		private var _stage:Stage;
		public function get stage():Stage
		{
			return _stage;
		}

		public function set stage(value:Stage):void
		{
			_stage = value;
		}
		
		private var _playVideo:Boolean = false;
		public function get playVideo():Boolean
		{
			return _playVideo;
		}
		
		public function set playVideo(value:Boolean):void
		{
			if(value == false)
			{
				_playVideo = value;
				dispatchEvent(new Event(STOP_VIDEO));
			}
			else
			{
				_playVideo = value;
				dispatchEvent(new Event(PLAY_VIDEO));	
			}
		}
		
		private var _fullscreen:Boolean = false;
		public function get fullscreen():Boolean
		{
			return _fullscreen;
		}
		
		public function set fullscreen(value:Boolean):void
		{
			if(_fullscreen == true){
				_fullscreen = false;
			}else{
				_fullscreen = true;
			}
			dispatchEvent(new Event(FULLSCREEN_CHANGE));	
		}
		
		private var _sound:Boolean = false;
		public function get sound():Boolean
		{
			return _sound;
		}
		
		public function set sound(value:Boolean):void
		{
			if(_sound == true){
				_sound = false;
			}else{
				_sound = true;
			}
			dispatchEvent(new Event(SOUND_CHANGE));	
		}
		
		
		private var _settings:Boolean = false;
		public function get settings():Boolean
		{
			return _settings;
		}
		
		public function set settings(value:Boolean):void
		{
			if(_settings == true){
				_settings = false;
			}else{
				_settings = true;
			}
			dispatchEvent(new Event(SETTING_CHANGE));
		}
		
		
		/** CONSTRUCTOR **/
		
		private static var instance:ApplicationModel;

		public static function getInstance():ApplicationModel {
			
			if( instance == null ) instance = new ApplicationModel( new Enforcer() );
			
			return instance;
		}
		public function ApplicationModel( o:Enforcer ){}
		
		public function loadGridData():void
		{
			loaddata("touchService.getAbout",onCallAbout);
		}
		
		private function loaddata(naamfunctie:String,uitvoerfunctie:Function):void{
			var amfCal:AMFCaller = new AMFCaller(naamfunctie, SettingsModel.getInstance().gateway);
			amfCal.addEventListener(PayloadEvent.PAYLOAD, uitvoerfunctie);
			amfCal.run();
		}
		
		private function loadDummyData():void
		{
			nodes = [];
			var node:NodeVO;
			totalRows = 20; 
			
			for(var i:uint=0; i<totalRows; i++)
			{
				node = new NodeVO();
				node.rowId = i;
				nodes.push(node);
			}
			
			dispatchEvent(new Event(GRID_DATA_LOADED));
		}
		
		private function onCall(e:PayloadEvent):void
		{
			_portfolioData = e.payload;
			var node:NodeVO;
			var teller:uint = 1;
			//color = 0xFF0000;
			for(var i:uint=0; i<_portfolioData.length; i++)
			{
				if(getNode(_portfolioData[i].project_id) == null)
				{
					node = new NodeVO();
					node.rowId = teller;
					node.project_id = _portfolioData[i].project_id;
					node.title = _portfolioData[i].titel;
					node.client = _portfolioData[i].client;
					node.omschrijving = _portfolioData[i].omschrijving;
					
					node.media.push(makeMediaVO(_portfolioData[i]));
					
					teller++;
					
					nodes.push(node);
					
				}
				else
				{
					node = getNode(_portfolioData[i].project_id);
					node.media.push(makeMediaVO(_portfolioData[i]));
				}
			}
			
			_totalRows = nodes.length;
			dispatchEvent(new Event(GRID_DATA_LOADED));
			trace(nodes.length);
		}
		
		private function onCallAbout(e:PayloadEvent):void
		{
			nodes = [];
			var node:NodeVO;
			node = new NodeVO();
			//node.rowId = 0;
			node.about = e.payload.about;
			node.color = e.payload.color;
			node.logo = e.payload.logo;
			setcolor(e.payload.color);
			node.clientRow = true;
			nodes.push(node);
			
			loaddata("touchService.getAllProjects",onCall);
		}
		
		private function setcolor(value:int):void{
			switch(value){
				case 1:
					ApplicationModel.getInstance().color = 0x6190a4;
					break;
				case 2:
					ApplicationModel.getInstance().color = 0x518564;
					break;
				case 3:
					ApplicationModel.getInstance().color = 0x585858;
					break;
				case 4:
					ApplicationModel.getInstance().color = 0x735a44;
					break;
			}
		}
		
		private function makeMediaVO(data:Object):MediaVo
		{
			var mediaVo:MediaVo = new MediaVo();
			mediaVo.url = data.url;
			
			// kijken of het video of images zijn
			var mediaURL:String = data.url;
			var extension:Array = mediaURL.split(".");
			
			if(extension[extension.length-1] == "f4v" || extension[extension.length-1] == "flv"){
				mediaVo.video = true;	
			}
			
			mediaVo.color = data.color;
			mediaVo.image_id = data.image_id;
			
			return mediaVo;
		}
		
		private function getNode(project_id:uint):NodeVO
		{
			var result:NodeVO = null;
			
			for(var i:uint=0; i<nodes.length; i++)
			{
				if(project_id ==  nodes[i].project_id)
				{
					result = nodes[i];
				}
			}
			
			return result;
		}
		
		private function onCall2(e:PayloadEvent):void
		{
			// haal grid data op uit database
			_portfolioData = e.payload;
			
			var allresults:Array = [];
			var node:NodeVO;
			var testcount:uint = 0;
			var tempArray:Array;
			
			for(var i:uint=0; i<_portfolioData.length; i++)
			{
				node = new NodeVO();
				node.rowId = allresults.length;
				node.project_id = _portfolioData[i].project_id;
				node.title = _portfolioData[i].titel;
				node.client = _portfolioData[i].client;
				node.omschrijving = _portfolioData[i].omschrijving;
				
				var mediaVo:MediaVo = new MediaVo();
				mediaVo.url = _portfolioData[i].url;
				
				// kijken of het video of images zijn
				var mediaURL:String = _portfolioData[i].url;
				var extension:Array = mediaURL.split(".");
				
				if(extension[extension.length-1] == "f4v" || extension[extension.length-1] == "flv"){
					mediaVo.video = true;	
				}
				
				mediaVo.color = _portfolioData[i].color;
				mediaVo.image_id = _portfolioData[i].image_id;
				
				node.media.push(mediaVo);
				allresults.push(node);
				
			}
				// duplicaten verwijderen
				// media toevoegen aan het model
				var filteredArray:Array = [];
				
				var found:Boolean = false;
				
				tempArray = [];
				tempArray.push(allresults[0].project_id);
				nodes.push(allresults[0]);
				
				for(i = 0; i < allresults.length; i++){
					
					for(var j:uint = 0; j<tempArray.length; j++){
						if(tempArray[j] == allresults[i].project_id) found = true;
					}
					if (!found){
						tempArray.push(allresults[i].project_id);
						nodes.push(allresults[i]);
					}else {
					}
					found = false;
				}
				
				for(var x:uint=0; x<tempArray.length; x++)
				{
					for (var c:uint=0; c<allresults.length; c++)
					{
						if(allresults[c].project_id == x){
							nodes[x].media.push(allresults[c].media[0]);
							nodes[x].rowId = x;
						}	
					}
				}

			_totalRows = nodes.length;
			dispatchEvent(new Event(GRID_DATA_LOADED));
		}
	}
}

internal class Enforcer{}